#!/bin/sh
# Copyright 2014 Timothy Edward Baldwin

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http:#www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


case "$1" in
 readd | add)
  ip link set dev "$2" up
  ip -6 addr add dev "$2" "$3"
  ip -6 route add metric "$4" default dev "$2"
  ip -6 route add table 101 default dev "$2"
  ip -6 rule add from "$3/96" table 101
  ip -6 route flush cache
 ;;
 remove)
  ip -6 addr delete dev "$2" "$3"
  ip -6 rule del from "$3/96" table 101
  ip -6 route del metric "$4" default dev "$2"
  ip -6 route del table 101 default dev "$2"
  ip -6 route flush cache
  ip link set dev "$2" down
  ;;
 change-metric)
  ip -6 route add metric "$4" default dev "$2"
  ip -6 route del metric "$3" default dev "$2"
  ;;
esac
